home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / xampp-win32-1.6.5-installer.exe / php / PEAR / SOAP / Value.php < prev    next >
Encoding:
PHP Script  |  2007-12-20  |  7.3 KB  |  244 lines

  1. <?php
  2. /**
  3.  * This file contains the code for converting values between SOAP and PHP.
  4.  *
  5.  * PHP versions 4 and 5
  6.  *
  7.  * LICENSE: This source file is subject to version 2.02 of the PHP license,
  8.  * that is bundled with this package in the file LICENSE, and is available at
  9.  * through the world-wide-web at http://www.php.net/license/2_02.txt.  If you
  10.  * did not receive a copy of the PHP license and are unable to obtain it
  11.  * through the world-wide-web, please send a note to license@php.net so we can
  12.  * mail you a copy immediately.
  13.  *
  14.  * @category   Web Services
  15.  * @package    SOAP
  16.  * @author     Dietrich Ayala <dietrich@ganx4.com> Original Author
  17.  * @author     Shane Caraveo <Shane@Caraveo.com>   Port to PEAR and more
  18.  * @author     Chuck Hagenbuch <chuck@horde.org>   Maintenance
  19.  * @author     Jan Schneider <jan@horde.org>       Maintenance
  20.  * @copyright  2003-2005 The PHP Group
  21.  * @license    http://www.php.net/license/2_02.txt  PHP License 2.02
  22.  * @link       http://pear.php.net/package/SOAP
  23.  */
  24.  
  25. require_once 'SOAP/Base.php';
  26.  
  27. /**
  28.  * SOAP::Value
  29.  *
  30.  * This class converts values between PHP and SOAP.
  31.  *
  32.  * Originally based on SOAPx4 by Dietrich Ayala
  33.  * http://dietrich.ganx4.com/soapx4
  34.  *
  35.  * @access  public
  36.  * @package SOAP
  37.  * @author  Shane Caraveo <shane@php.net> Conversion to PEAR and updates
  38.  * @author  Dietrich Ayala <dietrich@ganx4.com> Original Author
  39.  */
  40. class SOAP_Value
  41. {
  42.     /**
  43.      * @var string
  44.      */
  45.     var $value = null;
  46.  
  47.     /**
  48.      * @var string
  49.      */
  50.     var $name = '';
  51.  
  52.     /**
  53.      * @var string
  54.      */
  55.     var $type = '';
  56.  
  57.     /**
  58.      * Namespace
  59.      *
  60.      * @var string
  61.      */
  62.     var $namespace = '';
  63.     var $type_namespace = '';
  64.  
  65.     var $attributes = array();
  66.  
  67.     /**
  68.      * @var string
  69.      */
  70.     var $arrayType = '';
  71.  
  72.     var $options = array();
  73.  
  74.     var $nqn;
  75.     var $tqn;
  76.  
  77.     /**
  78.      * Constructor.
  79.      *
  80.      * @param string $name       Name of the SOAP value {namespace}name.
  81.      * @param mixed $type        SOAP value {namespace}type. Determined
  82.      *                           automatically if not set.
  83.      * @param mixed $value       Value to set.
  84.      * @param array $attributes  Attributes.
  85.      */
  86.     function SOAP_Value($name = '', $type = false, $value = null,
  87.                         $attributes = array())
  88.     {
  89.         // Detect type if not passed.
  90.         $this->nqn =& new QName($name);
  91.         $this->name = $this->nqn->name;
  92.         $this->namespace = $this->nqn->namespace;
  93.         $this->tqn =& new QName($type);
  94.         $this->type = $this->tqn->name;
  95.         $this->type_prefix = $this->tqn->ns;
  96.         $this->type_namespace = $this->tqn->namespace;
  97.         $this->value =& $value;
  98.         $this->attributes = $attributes;
  99.     }
  100.  
  101.     /**
  102.      * Serializes this value.
  103.      *
  104.      * @param SOAP_Base $serializer  A SOAP_Base instance or subclass to
  105.      *                               serialize with.
  106.      *
  107.      * @return string  XML representation of $this.
  108.      */
  109.     function serialize(&$serializer)
  110.     {
  111.         return $serializer->_serializeValue($this->value,
  112.                                             $this->name,
  113.                                             $this->type,
  114.                                             $this->namespace,
  115.                                             $this->type_namespace,
  116.                                             $this->options,
  117.                                             $this->attributes,
  118.                                             $this->arrayType);
  119.     }
  120.  
  121. }
  122.  
  123. /**
  124.  * This class converts values between PHP and SOAP. It is a simple wrapper
  125.  * around SOAP_Value, adding support for SOAP actor and mustunderstand
  126.  * parameters.
  127.  *
  128.  * Originally based on SOAPx4 by Dietrich Ayala
  129.  * http://dietrich.ganx4.com/soapx4
  130.  *
  131.  * @access  public
  132.  * @package SOAP
  133.  * @author  Shane Caraveo <shane@php.net> Conversion to PEAR and updates
  134.  * @author  Dietrich Ayala <dietrich@ganx4.com> Original Author
  135.  */
  136. class SOAP_Header extends SOAP_Value
  137. {
  138.     /**
  139.      * Constructor
  140.      *
  141.      * @param string $name             Name of the SOAP value {namespace}name.
  142.      * @param mixed $type              SOAP value {namespace}type. Determined
  143.      *                                 automatically if not set.
  144.      * @param mixed $value             Value to set
  145.      * @param integer $mustunderstand  Zero or one.
  146.      * @param mixed $attributes        Attributes.
  147.      */
  148.     function SOAP_Header($name = '', $type, $value, $mustunderstand = 0,
  149.                          $attributes = array())
  150.     {
  151.         if (!is_array($attributes)) {
  152.             $actor = $attributes;
  153.             $attributes = array();
  154.         }
  155.  
  156.         parent::SOAP_Value($name, $type, $value, $attributes);
  157.  
  158.         if (isset($actor)) {
  159.             $this->attributes['SOAP-ENV:actor'] = $actor;
  160.         } elseif (!isset($this->attributes['SOAP-ENV:actor'])) {
  161.             $this->attributes['SOAP-ENV:actor'] = 'http://schemas.xmlsoap.org/soap/actor/next';
  162.         }
  163.         $this->attributes['SOAP-ENV:mustUnderstand'] = (int)$mustunderstand;
  164.     }
  165.  
  166. }
  167.  
  168. /**
  169.  * This class handles MIME attachements per W3C's Note on Soap Attachements at
  170.  * http://www.w3.org/TR/SOAP-attachments
  171.  *
  172.  * @access  public
  173.  * @package SOAP
  174.  * @author  Shane Caraveo <shane@php.net> Conversion to PEAR and updates
  175.  */
  176. class SOAP_Attachment extends SOAP_Value
  177. {
  178.     /**
  179.      * Constructor.
  180.      *
  181.      * @param string $name      Name of the SOAP value <value_name>
  182.      * @param string $type      The attachment's MIME type.
  183.      * @param string $filename  The attachment's file name. Ignored if $file
  184.      *                          is provide.
  185.      * @param string $file      The attachment data.
  186.      */
  187.     function SOAP_Attachment($name = '', $type = 'application/octet-stream',
  188.                              $filename, $file = null)
  189.     {
  190.         parent::SOAP_Value($name, null, null);
  191.  
  192.         if (!isset($GLOBALS['SOAP_options']['Mime'])) {
  193.             $this->options['attachment'] = PEAR::raiseError('Mail_mime is not installed, unable to support SOAP Attachements');
  194.             return;
  195.         }
  196.  
  197.         $filedata = ($file === null) ? $this->_file2str($filename) : $file;
  198.         $filename = basename($filename);
  199.         if (PEAR::isError($filedata)) {
  200.             $this->options['attachment'] = $filedata;
  201.             return;
  202.         }
  203.  
  204.         $cid = md5(uniqid(time()));
  205.  
  206.         $this->attributes['href'] = 'cid:' . $cid; 
  207.  
  208.         $this->options['attachment'] = array('body' => $filedata,
  209.                                              'disposition' => $filename,
  210.                                              'content_type' => $type,
  211.                                              'encoding' => 'base64',
  212.                                              'cid' => $cid);
  213.     }
  214.  
  215.     /**
  216.      * Returns the contents of the given file name as string.
  217.      *
  218.      * @access private
  219.      *
  220.      * @param string $file_name  The file location.
  221.      *
  222.      * @return string  The file data or a PEAR_Error.
  223.      */
  224.     function _file2str($file_name)
  225.     {
  226.         if (!is_readable($file_name)) {
  227.             return PEAR::raiseError('File is not readable: ' . $file_name);
  228.         }
  229.  
  230.         if (function_exists('file_get_contents')) {
  231.             return file_get_contents($file_name);
  232.         }
  233.  
  234.         if (!$fd = fopen($file_name, 'rb')) {
  235.             return PEAR::raiseError('Could not open ' . $file_name);
  236.         }
  237.         $cont = fread($fd, filesize($file_name));
  238.         fclose($fd);
  239.  
  240.         return $cont;
  241.     }
  242.  
  243. }
  244.